﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CocisDAL;
using CocisModel;
using CocisModel.Common;
using System.Data;
using CocisWeb.src;

namespace CocisWeb.ManageAsset
{
    public partial class AddEnclosure : BasePage
    {
        public AssetDAL dal = new AssetDAL();
        public LogsDAL logs = new LogsDAL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.BindDropDownList();
            }
        }

        public void BindDropDownList()
        {
            this.ddlStatus.DataSource = dal.GetStatus();
            this.ddlStatus.DataTextField = "Status";
            this.ddlStatus.DataValueField = "StatusID";
            this.ddlStatus.DataBind();

            this.ddlSiteCode.DataSource = dal.GetSiteCode();
            this.ddlSiteCode.DataTextField = "SiteCode";
            this.ddlSiteCode.DataValueField = "SiteCodeID";
            this.ddlSiteCode.DataBind();

            this.ddlVendorName.DataSource = dal.GetVendor();
            this.ddlVendorName.DataTextField = "VendorName";
            this.ddlVendorName.DataValueField = "VendorID";
            this.ddlVendorName.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Asset.tbEnclosureDataTable dt = new Asset.tbEnclosureDataTable();
            Asset.tbEnclosureRow rows = dt.NewtbEnclosureRow();
            //Guid gd = new Guid();
            //rows.EnclosureID = gd;
            rows.AssetNo = this.txtAssetNo.Text;
            rows.EnclosureName = this.txtEnclosureName.Text;
            rows.StatusID = int.Parse(this.ddlStatus.SelectedValue.ToString());
            rows.Brand = this.txtBrand.Text;
            rows.SiteCodeID = int.Parse(this.ddlSiteCode.SelectedValue.ToString());
            rows.Floors = this.txtFloors.Text;
            rows.SN = this.txtSN.Text;
            rows.UUID = this.txtUUID.Text;
            rows.PartNumber = this.txtPartNumber.Text;
            rows.WarrantyStartDate = DateTime.Parse(this.txtWarrantyStartDate.Text);
            rows.WarrantyEndDate = DateTime.Parse(this.txtWarrantyEndDate.Text);
            rows.VendorID = int.Parse(this.ddlVendorName.SelectedValue.ToString());

            dt.Rows.Add(rows);
            dt.AcceptChanges();

            int temp = dal.AddEnclosure(dt);
            if (temp > 0)
            {
                string ip = Request.UserHostAddress.ToString();
                int t = logs.AddNewLogsInfo("insert new Enclosure", LoginUser.UserName, ip);

                ClientScript.RegisterStartupScript(this.GetType(), "a", "<script language=javascript>alert('添加成功！')</script>");
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "b", "<script language=javascript>alert('添加失败！')</script>");
            }
        }

    }
}